import Http, { HttpResponse } from './Http';
export default class PageService {
    protected _name: string = '';
    protected _base_url: string = '/api';

    constructor(base_url?: string) {
        this._base_url = base_url || this._base_url;
    }

    public get name() {
        return this._name;
    }

    public set name(value: string) {
        this._name = value;
    }

    public get baseUrl() {
        return this._base_url || this.constructor.name;
    }

    public set baseUrl(value: string) {
        this._base_url = value;
    }

    async fetch(url: string): Promise<HttpResponse> {
        return await fetch(this._base_url + url);
    }

    async get(url: string, params?: {}, isEncode?: boolean): Promise<HttpResponse> {
        return await Http.get(this._base_url + url, params, isEncode);
    }

    async post(url: string, args?: {}): Promise<HttpResponse> {
        return await Http.post(this._base_url + url, args);
    }

    async put(url: string, args?: {}): Promise<HttpResponse> {
        return await Http.put(this._base_url + url, args);
    }

    async delete(url: string, params?: {}): Promise<HttpResponse> {
        return await Http.delete(this._base_url + url, params);
    }

    async formdata(url: string, formdata?: any, callback?: (response: Response) => void): Promise<HttpResponse> {
        return await Http.formdata(this._base_url + url, formdata, callback);
    }

    async uploads(url: string, formdata?: any, onProgress?: (progress: number, event: Event) => void): Promise<HttpResponse> {
        return await Http.uploads(this._base_url + url, formdata, onProgress);
    }

    async readStream(url: string, body?: Record<string, any>, successCallback?: (data: any) => void, errorCallback?: (err: any) => void): Promise<any> {
        return await Http.readStream(this._base_url + url, body, successCallback, errorCallback);
    }
}